<?php

/******************************************
 * File:			lib_string.php
 * Version:			1.0.0
 * Last modified:	25/10/2009
 * Last modifier:	Hugo Lavigne
 *****************************************/

Function str_to_word_tab($str, $sep)
{
	$i = 0;
  	$j = 0;
  	$start = 0;
  	$len = strlen($str);
  	while ($i <= $len)
  	{
    	if ((substr($str, $i, 1) == $sep) || ($i == $len))
    	{
			$size = $i - $start;
			$res[$j] = substr($str, $start, $size);
			$start = $i;
			$start++;
			$j++;
    	}
		$i++;
	}
	return ($res);
}

Function trunc_str_right($str, $nb)
{
  $res = $str;
  if (strlen($str) > $nb)
  {
    $res = substr($str, 0, ($nb-3));
    $res .= "...";
  }
  return ($res);
}

Function check_quote_in($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if ($str[$i] == '"')
			$res .= '\"';
		else
			$res .= $str[$i];
		$i++;
	}
	return ($res);
}

Function check_squote_in($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if ($str[$i] == "'")
			$res .= "\'";
		else
			$res .= $str[$i];
		$i++;
	}
	return ($res);
}

Function check_space_in($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if ($str[$i] == ' ')
			$res .= "&nbsp;";
		else
			$res .= $str[$i];
		$i++;
	}
	return ($res);
}

Function check_tab_in($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if ($str[$i] == "\t")
			$res .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		else
			$res .= $str[$i];
		$i++;
	}
	return ($res);
}

Function check_tab_out($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if (substr($str, $i, 6) == "&nbsp;")
			$i += 5;
		else
			$res .= $str[$i];
		$i++;
	}
	return ($res);
}

Function check_return_in($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if ($str[$i] == "\n")
			$res .= "<br>";
		elseif (substr($str, $i, 4) == "<br>")
			$i += 3;
		else
			$res .= $str[$i];
		$i++;
	}
	return ($res);
}

Function check_return_out($str)
{
	$i = 0;
	$len = strlen($str);
	$res = "";
	
	while ($i < $len)
	{
		if (substr($str, $i, 4) == "<br>")
			$i += 4;
		$res .= $str[$i];
		$i++;
	}
	return ($res);
}

?>